package nsn.message.aggregator.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashSet;
import java.util.Set;

/**
 * 
 * 
 * @author Lukasz Nowak
 * @since 10-03-2011
 * 
 */
public abstract class AbstractSourceType implements ISourceType {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7861426340602722784L;
	final private Set<ISource> _sources = new HashSet<ISource>();
	private String _name = "";

	/**
	 * 
	 */
	protected final transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	
	@Override
	public String getName() {
		return _name;
	}


	@Override
	public Set<ISource> getSources() {
		return _sources;
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj instanceof AbstractSourceType) {
			final AbstractSourceType type = (AbstractSourceType) obj;
			return _name.equals(type._name);
		}
		this.hashCode();
		return false;
	}

	@Override
	public int hashCode() {
		return _name.hashCode();
	}	
	
	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(listener);

	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(listener);

	}

	/**
	 *
	 * @param name
	 */
	public void setName(final String name) {
		_name = name;
	}

	/**
	 * 
	 * 
	 * @param source
	 */
	public void delete(final ISource source) {
		getSources().remove(source);
		fireChangeEvent("SourceDeleted", null, source);
	}


	/**
	 *
	 * @param string
	 * @param oldValue
	 * @param newValue
	 */
	public void fireChangeEvent(final String string, final Object oldValue, final Object newValue) {
		propertyChangeSupport.firePropertyChange(string, oldValue, newValue);

		
	}
	

}
